package com.example.taobao

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.gson.Gson
import kotlinx.android.synthetic.main.fragment_mine.*
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import java.net.URLEncoder
import kotlin.concurrent.thread

class Fragment_mine : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_mine,container,false)
        return view
    }

    override fun onResume() {
        inirdata()
        super.onResume()
    }

    override fun onDestroy() {
        val editor = activity?.getSharedPreferences("data", Context.MODE_PRIVATE)?.edit()
        editor?.clear()
        editor?.commit()
        super.onDestroy()
    }

    override fun onStart() {
        inirdata()
        super.onStart()
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val tag = activity?.intent?.getIntExtra("tag", 0)
        inirdata()
        val gson = Gson()
        var msglistd:ArrayList<MsgContentBean> = ArrayList<MsgContentBean>()
        msglistd.add(MsgContentBean(2,"喜迎双旦，会员惊喜连连！"))
        msglistd.add(MsgContentBean(1,"您的商品已经发货了~不要催！"))
        msglistd.add(MsgContentBean(1,"黑五狂欢，爆款5折其秒"))
        msglistd.add(MsgContentBean(1,"感谢有你，感恩节就要送福利！"))
        msglistd.add(MsgContentBean(2,"我买的东西怎么还没发货"))
        msglistd.add(MsgContentBean(2,"知道了！"))
        val inputString = gson.toJson(msglistd)

        inirdata()
        myorder11.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }
        quanbudingdan.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }

        shouhuodiz.setOnClickListener {
            val intent = Intent(activity, Myaddress::class.java)
            startActivity(intent)
        }
        daifukuan1.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }
        daifukuan2.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }
        daifukuan3.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }
        daifukuan4.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }
        daifukuan5.setOnClickListener {
            val intent = Intent(activity, Myorder::class.java)
            startActivity(intent)
        }
        wodexinx.setOnClickListener {
            val intent = Intent(activity,myxinxi::class.java)
            startActivity(intent)
        }
        myname1.setOnClickListener {
            val intent = Intent(activity, Login::class.java)
            startActivity(intent)
        }
        mynumber1.setOnClickListener {
            val intent = Intent(activity, Login::class.java)
            startActivity(intent)
        }

        mypic1.setOnClickListener {
            val intent = Intent(activity, Login::class.java)
            startActivity(intent)
        }
        wodesc.setOnClickListener {
            val intent = Intent(activity, Mylove::class.java)
            startActivity(intent)
        }
        tv_btn.setOnClickListener {
            thread {
                try {
                    val client = OkHttpClient()
                    val requestBody = FormBody.Builder()
                        .add("name", URLEncoder.encode("数码用品店","utf-8"))
                        .add("history", URLEncoder.encode(inputString,"utf-8"))
                        .add("mas", URLEncoder.encode("收到信息了！","utf-8"))
                        .add("time", URLEncoder.encode("1月1日","utf-8"))
                        .add("username", URLEncoder.encode("Eurus","utf-8"))
                        .add("megpic",URLEncoder.encode("https://gw.alicdn.com/bao/uploaded/i1/2206633145364/O1CN01jSMRXS1pUl90kUrP5_!!0-item_pic.jpg","utf-8"))
                        .build()
                    val request = Request.Builder()
                        .url("http://192.168.43.140:8080/aaa_war_exploded/megadd.jsp")
                        .post(requestBody)
                        .build()
                    val respone = client.newCall(request).execute()
                    val responeData = respone.body?.string()
                }
                catch (e: Exception)
                {
                    e.printStackTrace()
                }
            }
        }
    }
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            inirdata()
        }
    }
    fun inirdata()
    {
        val prefs =activity?.getSharedPreferences("data", Context.MODE_PRIVATE)
        val name = prefs?.getString("name","未登录")
        val pic = prefs?.getString("pic","@drawable/mypic1")
        val zhangh = prefs?.getString("zhangh","无")
        val shouc = prefs?.getString("shouc","0")
        val guanz = prefs?.getString("guanz","0")
        val zuji = prefs?.getString("zuji","0")
        val hongbao = prefs?.getString("hongbao","0")
        val dengluzhuangtai = prefs?.getString("dengluzhuangtai","缺失")
        myname1.text = name
        mynumber1.text = zhangh
        sc.text = shouc
        gz.text=guanz
        zj.text = zuji
        hb.text = hongbao
    }

}